<template>
	<view>
		<!-- 头部组件 -->
		<create-group-head @changeActive="changeActive"></create-group-head>
		<view class="head-zw"></view>
		<!-- 核心组件 -->
		<create-group-type v-if="showActive == 0" @changeType="changeType"></create-group-type>
		<!-- <create-group-list v-if="showActive == 1" :showList="friendList"></create-group-list> -->
		<create-group-info v-if="showActive == 1" @changeGroupName="changeGroupName" @changeHead="changeGroupHead">
		</create-group-info>
		<create-group-list v-if="showActive == 2" @changeUsers="changeUsers"></create-group-list>

		<view class="create-bottom">
			<view class="box">
				<button @click="createGroupFun">立即创建</button>
			</view>
		</view>
	</view>
</template>

<script>
	import createGroupHead from './child/create-group-head.vue'
	import createGroupType from './child/create-group-type.vue'
	import createGroupList from './child/create-group-list.vue'
	import createGroupInfo from './child/create-group-info.vue'

	import {
		createGroup
	} from '@/api/http/msg/group.js'

	import myfun from '@/utils/time/time.js'

	// import msglist from '@/utils/msg/msglist.js'

	import websocket from '@/utils/msg/websocketUtils.js'

	import {
		getNowTime
	} from '@/utils/time/time.js'

	//消息对象
	import {
		msgHomeData,
		msgChatData
	} from '@/object/msg.js'

	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex'
	
	import websocketInit from '@/mixins/websocket_init.js'

	export default {
		mixins: [websocketInit],
		components: {
			createGroupHead,
			createGroupType,
			createGroupList,
			createGroupInfo
		},
		data() {
			return {
				showActive: 0,
				//聊天类型
				groupType: '亲朋',
				friendList: [],
				users: '',
				groupName: uni.getStorageSync('nickname') + '创建的群聊',
				groupHead: uni.getStorageSync('head')
			}
		},
		methods: {
			...mapActions('m_chat', ['setMsgList', 'addChatData']),
			changeActive(e) {
				//console.log(e)
				this.showActive = e
			},
			changeType(item) {
				this.groupType = item.name
				//console.log(this.groupType)
			},
			//获取用户数据
			getFriendList() {
				this.friendList = friend
			},
			changeGroupName(groupName) {
				//console.log(groupName)
				this.groupName = groupName
			},
			changeGroupHead(groupHead) {
				this.groupHead = groupHead
			},
			changeUsers(users){
				var newUsers = ''
				if(users.length != 0){
					for (var i = 0; i < users.length; i++) {
						newUsers += users[i].userId
						if(i != users.length - 1){
							newUsers += ','
						}
					}
				}
				this.users = newUsers
			},
			
			//创建群组
			createGroupFun() {
				var that = this
				var reqData = {
					typeName: this.groupType,
					userIds: this.users,
					groupName: this.groupName,
					groupHead: this.groupHead
				}
				//console.log(reqData)
				createGroup(reqData).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					var result = res.data.busInfo
					//发送消息到netty
					var createGroupData = {
						type: this.$chatType.CREATE_GROUP,
						msg: {
							createId: uni.getStorageSync('userId'),
							groupId: result.gid,
							groupName: result.groupName,
							createGroupName: 'group_' + result.gid,
							userIds: that.users,
							groupHead: result.groupHead
						}
					}

					uni.myWebsocket.socketTask.send({
						data: JSON.stringify(createGroupData),
						async success() {
							var resData = {
								id: res.data.busInfo.gid,
								nickname: res.data.busInfo.groupName,
								head: res.data.busInfo.groupHead,
								type: 3
							}
							var groupData = JSON.stringify(resData)

							// var nowTime = getNowTime()

							// //id, type, name, head, news, time, msgNum, msgType
							// var addMsgHomeData = msgHomeData(
							// 	res.data.busInfo.gid,
							// 	3,
							// 	res.data.busInfo.groupName,
							// 	res.data.busInfo.groupHead,
							// 	'创建群聊',
							// 	nowTime,
							// 	0,
							// 	6
							// )
							// that.setMsgList(addMsgHomeData)

							// //msgId, tip, news, sendId, sendHead, sendNickname, msgType, time
							// var addMsgChatData = msgChatData(
							// 	1,
							// 	1,
							// 	'创建群聊',
							// 	uni.getStorageSync('userId'),
							// 	uni.getStorageSync('head'),
							// 	uni.getStorageSync('nickname'),
							// 	6,
							// 	nowTime,
							// 	3,
							// 	res.data.busInfo.gid
							// )

							// that.addChatData(addMsgChatData)


						},
						fail(e) {
							uni.$showMsg('创建失败！');
							//this.connectSocketInit()
						}
					});

					setTimeout(() => {
						uni.switchTab({
							url: '/pages/main/message/message'
						})
					}, 100)

				})
			},

			// connectSocketInit() {
			// 	const urlWss = 'ws://' + this.$basePort + ':18082/ws'
			// 	const wss = new websocket(urlWss, 5000)

			// 	uni.myWebsocket = wss
			// }
		},
		created() {
			//this.getFriendList()
		}
	}
</script>

<style lang="scss" scoped>
	.head-zw {
		width: 100%;
		height: 100rpx;
	}

	.create-bottom {
		width: 100%;
		position: fixed;
		z-index: 9;
		background-color: #FBFBFB;
		bottom: 0;
		height: 100rpx;

		button {
			background-color: #7DE6FC;
			width: 80%;
			border-radius: 45rpx;
			margin-left: 10%;
			height: 80rpx;
			line-height: 80rpx;
			margin-top: 10rpx;
			color: #fff;
			font-size: 42rpx;
		}
	}
</style>